package com.cfets.annualAssessment.securityConfig;

import com.cfets.annualAssessment.entity.Resource;
import com.cfets.annualAssessment.entity.Role;
import com.cfets.annualAssessment.mapper.ResourceMapper;
import com.cfets.annualAssessment.service.ResourceService;
import com.cfets.annualAssessment.service.RoleService;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by liusonglin on 2016/11/25.
 */
public class MyInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {

    LoadingCache<String, Collection<ConfigAttribute>> conditionList;

    @Autowired
    private RoleService resourceService;




    public MyInvocationSecurityMetadataSource(){


        CacheLoader<String,Collection<ConfigAttribute>> resourceListLoad = new CacheLoader<String,Collection<ConfigAttribute>>() {
            @Override
            public Collection<ConfigAttribute> load(String key) {
                Collection<ConfigAttribute> atts = new ArrayList<>();
                List<Role> roleList = resourceService.findRoleIdByUrl(key);
                ConfigAttribute ca;
                for(Role item:roleList){
                    ca = new SecurityConfig(item.getId().toString());
                    atts.add(ca);
                }
                return atts;
            }
        };
        conditionList = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(1, TimeUnit.MINUTES)
                .build(resourceListLoad);
    }


    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        // 将参数转为url
        String url = ((FilterInvocation)o).getRequest().getRequestURI();
        try {
            if(url.contains(".css")||url.contains(".js")||url.contains(".woff2")||url.contains(".woff")||
                    url.contains(".ttf")||url.contains(".ico")||url.contains("login")||url.contains(".jpg")
                    ||url.contains(".png")){
                return null;
            }
            return conditionList.get(url);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
